package merge;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Main {
	public static void main(String[] args) throws SQLException {
		Connection con=DAO.DbcpConnection.getConnection();
		PreparedStatement pst=con.prepareStatement("select * from all_detail_total where arno=? and brand=?");
		PreparedStatement pInserSt=con.prepareStatement("insert into fromuukeyTouukey (fromuukey,touukey) value (?,?)");
		Statement st=con.createStatement();
		String sql="select * from all_detail1";
		ResultSet rs=st.executeQuery(sql);
		int i=0;
		while (rs.next()) {
			System.out.println(i++);
			String arno=rs.getString("arno");
			long uukey=rs.getLong("uukey");
			String brand=rs.getString("brand");
			if(arno!=null&&brand!=null){
				pst.setString(1, arno);
				pst.setString(2, brand);
				ResultSet prs=pst.executeQuery();
				long uukeyTo=0;
				if(prs.next()){
					uukeyTo=prs.getLong("uukey");
				}
				insertUUkey(uukey,uukeyTo,pInserSt);
			}
			else {
				insertUUkey(uukey,0,pInserSt);
			}
		}
		
		pInserSt.executeBatch();
		
	}

	private static void insertUUkey(long uukey, long uukeyTo,PreparedStatement pInserSt) throws SQLException {
		pInserSt.setLong(1, uukey);
		pInserSt.setLong(2, uukeyTo);
		pInserSt.addBatch();
		
	}

}
